<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-10
 * Time: 17:55
 */

namespace app\api\controller;

use app\service\LogService;
use app\service\OrderService;
use app\service\UserService;
use app\service\WechatService;
use app\service\PayService;
use app\common\model\Order as OrderModel;
use app\common\model\User as UserModel;
use EasyWeChat\Factory;

class Pay extends Base
{

  public function __construct () {
    parent::__construct();
  }

  /* 订单支付 */
  public function order () {
    $userInfo = UserModel::getOne(['id'=>$this->uid]);
    $input = input();
    if(!$input['id']) {
      $this->ajax(202,'订单不存在');
    }
    $order = OrderModel::getOne([
      'id' => $input['id']
    ]);
    if(!$order) {
      $this->ajax(202,'订单不存在');
    }
    if($order['checked']!=0) {
      $this->ajax(202,'当前订单状态无法支付');
    }
    $use_money = $use_score = false;
    if($input['use_money']) {
      $use_money = true;
    }
    if($input['use_score']) {
      $use_score = true;
    }
    if($input['use_cash']) {
      $use_cash = true;
    }
    //1主商城 2积分商城 3分销商城 4用于升级的商品
    if($order['shop_type']==1) {
      $use_score = false;
    }elseif($order['shop_type']==2) {
      $maxScore = 0;
      $goods_list = json_decode($order['goods_list'],true);
      foreach($goods_list as $v) {
          $maxScore += $v['price']*$v['score_bili']*$v['goods_num'];
      }
      $maxScore = round($maxScore,2);
    }elseif($order['shop_type']==3) {
      $use_score = false;
    }elseif($order['shop_type']==4) {
      $use_score = false;
    }
    $order['pay_money'] = $order['goods_money'] + $order['express_money'];//重置支付金额
     //积分抵扣
    if($use_score && $order['pay_money']>0) {
      //$maxScore = $order['pay_money']-$order['express_money'];//运费不能积分抵扣
      if($userInfo['score']>=$maxScore) {
        $order['score_money'] = $maxScore;
        $order['pay_money'] -= $order['score_money'];
      }elseif($userInfo['score']>0) {
        $order['score_money'] = $userInfo['score'];
        $order['pay_money'] -= $order['score_money'];
      }
    }

    //cash抵扣
    if($use_cash && $order['pay_money']>0) {
      if($userInfo['cash']>=$order['pay_money']) {
        $order['cash_money'] = $order['pay_money'];
        $order['pay_money'] -= $order['cash_money'];
      }elseif($userInfo['cash']>0) {
        $order['cash_money'] = $userInfo['cash'];
        $order['pay_money'] -= $order['cash_money'];
      }
    }
    
    //佣金抵扣
    if($use_money && $order['pay_money']>0) {
      if($userInfo['money']>=$order['pay_money']) {
        $order['yongjin_money'] = $order['pay_money'];
        $order['pay_money'] -= $order['yongjin_money'];
      }elseif($userInfo['money']>0) {
        $order['yongjin_money'] = $userInfo['money'];
        $order['pay_money'] -= $order['yongjin_money'];
      }
    }
   
    //更新订单字段
    if($use_money||$use_score||$use_cash) {
      OrderModel::where('id',$order['id'])->update([
        'pay_money' => $order['pay_money'],
        'yongjin_money' => $order['yongjin_money'],
        'score_money' => $order['score_money'],
        'cash_money' => $order['cash_money'],
      ]);
    }
    
   

    //需要在线支付
    if($order['pay_money']>0) {
      $orderConfig = [
        'body' => $order['title'],
        'out_trade_no' => $order['order_sn'],
        'total_fee' => $order['pay_money']*100,
       // 'spbill_create_ip' => '123.12.12.123', // 可选，如不传该参数，SDK 将会自动获取相应 IP 地址
        'notify_url' => request()->domain()."/notify/wechat/mall_id/".$this->mall_id, 
        'trade_type' => 'JSAPI',
        //'openid' => UserModel::uid2openid($userInfo['id'])
        'openid' => $this->member['openid_main']
      ];
      $jsapi = PayService::S(['mall_id'=>1])->wechatJssdk($orderConfig); 
      //$jsapi = SbPayService::S()->wechatJssdk($orderConfig);

      /* $data = [
        'appId' => $result['appid'],
        'timeStamp' => time(),
        'nonceStr' => $result['nonce_str'],
        'signType' => 'MD5',
        'paySign' => $result['sign'],
        'package' => "prepay_id={$result['prepay_id']}"
      ]; */
      $this->ajax(200,'success',[
        'pay_money' => $order['pay_money'],
        'jsapi' => $jsapi
      ]);
    }
    OrderService::S()->changeOrder($order,[
      'checked' => 1,
      'pay_type' => 2
    ]);

    $this->ajax(200,'success',[
      'pay_money' => 0,
    ]);
  }


  

  /* 充值 */

}